export default {

    print_label: function () {
        var btns = [];
        btns.push(lan['Print_Carton_Label']); //箱条码
        btns.push(lan['Print_Pallet_Label']);
        btns.push(lan['Print_SN_Label']);

        var that = this;
        api.actionSheet({
            title: lan['Tips'],
            cancelTitle: lan['Cancel'],
            buttons: btns
        }, function (ret, err) {
            var index = ret.buttonIndex;
            var key = index - 1;

            if (btns[key] == lan['Print_Carton_Label']) {

                print_type = 2;
                that.print_carton();
            }
            if (btns[key] == lan['Print_Pallet_Label']) {
                if (modeldayin == "zebra") {
                    api.toast({
                        msg: "Disabled in this printer",
                        location: 'middle'
                    });
                    return false;
                }
                print_type = 1;
                that.print_pallet();
            }
            if (btns[key] == lan['Print_SN_Label']) {
                print_type = 1;
                that.submit_sncode();
            }
        });
    },
    async print_carton() {
        showProgress(lan['Loading']);
        print_tags = [];
        for (i in this.list) {
            if (this.list[i].print) {
                if (this.list[i].LabelNum.indexOf("TR") == -1) {
                    if (this.list[i].is_load == false) {
                        // this.list[i].CountryCode_c = "中國 China";
                        this.list[i].ExpirationDate = await this.load_ExpirationDate(this.list[i]);
                        this.list[i].ProdCode = await this.load_ProdCode(this.list[i]);
                        this.list[i].is_load = true;
                    }
                    print_tags.push(this.list[i]);
                }
            }
        }
        hideProgress();
        if (print_tags.length > 0) {
            open_printpage2(print_tags, 1, 1, 2,0);
        } else {
            api.toast({
                msg: lan['No_Data_Submit'],
                location: 'middle'
            });
        }
    },
    print_pallet: function () {

        print_tags = [];
        for (i in this.list) {
            if (this.list[i].print) {
                if (this.list[i].LabelNum.indexOf("TR") != -1) {
                    print_tags.push(this.list[i]);
                }
            }
        }
        if (print_tags.length > 0) {
            open_printpage(print_tags, 1, 2);
        } else {
            api.toast({
                msg: lan['No_Data_Submit'],
                location: 'middle'
            });
        }
    },
    async submit_sncode() {
        print_tags = [];
        showProgress(lan['Loading']);
        print_tags = [];
        for (i in this.list) {
            if (this.list[i].print) {
                if (this.list[i].LabelNum.indexOf("TR") == -1) {
                    if (this.list[i].is_sn_load == false) {
                        this.list[i].SerialNum = await this.loadSerialNum(this.list[i]);
                    }
                    print_tags = print_tags.concat(this.list[i].SerialNum);
                }
            }
        }
        hideProgress();


        if (print_tags.length > 0) {
            open_printpage(print_tags, 1, 3);
        } else {
            api.toast({
                msg: lan['No_Data_Submit'],
                location: 'middle'
            });
        }



    },
    load_ExpirationDate(item) {

        return new Promise((resolve) => {

            api.ajax({
                url: server + 'Erp.BO.LotSelectUpdateSvc/LotSelectUpdates(' + userinfo.Company + ',' + item.PartNum + ',' + item.LotNum + ')',
                method: 'get',
                returnAll: false,
                headers: {
                    'Authorization': userinfo.Token,
                    'Content-Type': 'application/json;charset=utf-8'
                },
                data: {}
            }, function (ret, err) {
                ajaxing = 0;
                console_debug(ret, err);
                //hideProgress();
                var ExpirationDate = "";
                if (ret) {
                    ExpirationDate = ret.ExpirationDate;
                }
                resolve(ExpirationDate);
            });



        })

    },
    load_ProdCode(item) {

        return new Promise((resolve) => {

            api.ajax({
                url: server + 'Erp.BO.PartSvc/Parts(' + userinfo.Company + ',' + item.PartNum + ')',
                method: 'get',
                returnAll: false,
                headers: {
                    'Authorization': userinfo.Token,
                    'Content-Type': 'application/json;charset=utf-8'
                },
                data: {}
            }, function (ret, err) {
                ajaxing = 0;
                console_debug(ret, "Erp.BO.PartSvc/Parts");
                var ProdCode = "";
                if (ret) {
                    ProdCode = ret.ProdCode;
                }
                resolve(ProdCode);

            });



        })

    },
    loadSerialNum(item) {

        return new Promise((resolve) => {

            var filter = "Company eq '" + userinfo.Company + "'";

            filter += " and LabelNum eq '" + item.LabelNum + "'";
            filter += " and SNStatus eq 'INVENTORY'";

            console_debug(filter, '');

            api.ajax({
                url: wmsserver + 'serialno/?filter=' + filter,
                method: 'get',
                returnAll: false,
                headers: {
                    'token': wmstoken,
                    'Content-Type': 'application/json;charset=utf-8'
                },
                data: {}
            }, function (ret, err) {
                ajaxing = 0;
                //alert(1);
                console_debug(ret, "loadSerialNum");
                hideProgress();
                var SerialNum = [];
                if (ret && ret[0]) {
                    ret = Upper_arr(ret);
                    for (i in ret) {
                        ret[i].tag = item;
                    }
                    SerialNum = SerialNum.concat(ret);
                }
                resolve(SerialNum);

            });


        })

    },
    loadTagsTransaction(item) {
        return new Promise((resolve) => {
            userinfo = get_user_info();
            var filter = "Company eq '" + userinfo.Company + "'";
            filter += " and LabelNum eq '" + item.LabelNum + "'";

            console_debug(filter, '');

            api.ajax({
                url: wmsserver + 'PackageTransaction/?filter=' + filter,
                method: 'get',
                returnAll: false,
                headers: {
                    'token': wmstoken,
                    'Content-Type': 'application/json;charset=utf-8'
                },
            }, function (ret, err) {

                var res = {};
                if (ret && ret[0]) {
                    ret = Upper_arr(ret);
                    res = ret[0];
                }
                resolve(res);

            });
        });
    },
    postTags(item) {
        return new Promise((resolve) => {
            
            api.ajax({
                url: wmsserver + 'packagestatus/',
                method: 'post',
                returnAll: false,
                dataType: 'text',
                headers: {
                    'token': wmstoken,
                    'Content-Type': 'application/json;charset=utf-8'
                },
                data: {
                    body: JSON.stringify(item)
                }
                //data: {values: data1}
            }, function (ret, err) {
                console_debug(ret, err);
                if (ret && parseInt(ret) == 200) {
                    resolve(true);
                } else {
                    api.toast({
                        msg: lan['label_Failed'],
                        location: 'middle'
                    });
                    resolve(false);
                }
    
            });


        });
    },
    updateTags(item) {
        return new Promise((resolve) => {
            
            api.ajax({
                url: wmsserver + 'packagestatus/',
                method: 'put',
                returnAll: false,
                dataType: 'text',
                headers: {
                    'token': wmstoken,
                    'Content-Type': 'application/json;charset=utf-8'
                },
                data: {
                    body: JSON.stringify(item)
                }
                //data: {values: data1}
            }, function (ret, err) {
                console_debug(ret, err);
                if (ret && parseInt(ret) == 200) {
                    resolve(true);
                } else {
                    api.toast({
                        msg: lan['label_Failed'],
                        location: 'middle'
                    });
                    resolve(false);
                }
    
            });


        });
    },
    postTagsTransaction(item) {
        return new Promise((resolve) => {
            
            api.ajax({
                url: wmsserver + 'PackageTransaction/',
                method: 'post',
                returnAll: false,
                headers: {
                    'token': wmstoken,
                    'Content-Type': 'application/json;charset=utf-8'
                },
                dataType: 'text',
                data: {
                    body: JSON.stringify(item)
                }
            }, function (ret, err) {
                console_debug(ret, err);
                if (ret && parseInt(ret) == 200) {
                    resolve(true);
                } else {
                    api.toast({
                        msg: lan['label_Failed'],
                        location: 'middle'
                    });
                    resolve(false);
                }
    
            });


        });
    },
    updateTagsTransaction(item) {
        return new Promise((resolve) => {
            
            api.ajax({
                url: wmsserver + 'PackageTransaction/',
                method: 'put',
                returnAll: false,
                headers: {
                    'token': wmstoken,
                    'Content-Type': 'application/json;charset=utf-8'
                },
                dataType: 'text',
                data: {
                    body: JSON.stringify(item)
                }
            }, function (ret, err) {
                console_debug(ret, err);
                if (ret && parseInt(ret) == 200) {
                    resolve(true);
                } else {
                    api.toast({
                        msg: lan['label_Failed'],
                        location: 'middle'
                    });
                    resolve(false);
                }
    
            });


        });
    },


}